import wx
from zope import interface
import os, sys

APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEWATCHER_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("must set environment variable SIMPLEWATCHER_INSTALL")
sys.path.append('%s/lib' % APPLICATION_INSTALL_DIR)

from watch_builder.gui.watchobjects import interfaces as watch_interfaces
from watch_builder.gui.watchobjects.base import SimpleWatcherDataObject


class BranchDataObject(SimpleWatcherDataObject):
    interface.implements(watch_interfaces.IBranchDataObject)

   
    def __init__(self, *args, **kwargs):
        super(BranchDataObject, self).__init__(*args, **kwargs)
        self.buildDefaults()
        self.setImage(kwargs.get('image', self._default_image))
        # set all kwarg attrs relevant to building the object's xml
        for (attr, xattr) in self._xml_build_attrs:
            setattr(self, attr, kwargs.get(attr, ''))
        self._children = []
        self.build_schema()


    def buildDefaults(self):
        # default image
        self._default_image = "branch_object.gif"
        # attributes that can be read to and from xml
        self._xml_build_attrs = [('branchName','//branch/@name'), ('packageName', '//branch/@package' ), ('klass', '//branch/@klass'),
                                 ('executePath', '//branch/execute_path')]
    
        # valid children, embedded resources, and only child for self.schema
        self._valid_children = []
        self._embedded_resources = [watch_interfaces.IExecutePathDataObject, watch_interfaces.ICommentDataObject]
        self._only_child = [watch_interfaces.IExecutePathDataObject]


    def guiObjXml(self):
        for attr, xattr in self._xml_build_attrs:
            if not hasattr(self, attr):
                setattr(self, attr, "")
        xml = """
        <branch
            name="%(branchName)s"
            package="%(packageName)s"
            klass="%(klass)s"
            >
        </branch>
        """ % self.__dict__
        return xml


    def watchBuilderXml(self):
        for attr, xattr in self._xml_build_attrs:
            if not hasattr(self, attr):
                setattr(self, attr, "")
        xml = """
        <branch
            name="%(branchName)s"
            package="%(packageName)s"
            klass="%(klass)s"
            >
          <execute_path>%(executePath)s</execute_path>
        </branch>
        """ % self.__dict__
        return xml

